package com.example.demo_yunpan.controller;

import com.example.demo_yunpan.entity.file;
import com.example.demo_yunpan.entity.folder;
import com.example.demo_yunpan.service.FolderService;
import com.example.demo_yunpan.service.HdfsFileService;
import org.apache.hadoop.fs.FileStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FolderController {
    @Autowired
    private FolderService folderService;
    @Autowired
    private HdfsFileService hdfsFileService;

    //查询同一目录下的文件夹和文件
    @PostMapping ("/getFirstFolder")
    public ResponseEntity<Map<String, Object>> getFirstFolder(@RequestParam String userId, @RequestParam String path){
        List<folder> folders = folderService.findFolderByuserId(userId,path);
        List<file> files = hdfsFileService.findFileByUserId(userId, path);

        //将查询结果放入HashMap
        Map<String, Object> result = new HashMap<>();
        result.put("folders",folders);
        result.put("files",files);
        return ResponseEntity.ok(result);
    }

    //查询同一用户所有文件和文件夹
    @PostMapping("/getAll/{id}")
    public ResponseEntity<Map<String, Object>> getAllFileAndFolder (@PathVariable String id,@RequestParam(required = false) String fileNameFuzzy,
                                                                    @RequestParam(required = false) String category){

        List<folder> folders = folderService.findAllByuserId(id,fileNameFuzzy,category);
        List<file> files = hdfsFileService.findAllByuserId(id,fileNameFuzzy,category);

        //将查询结果放入HashMap
        Map<String, Object> result = new HashMap<>();
        result.put("folders",folders);
        result.put("files",files);
        return ResponseEntity.ok(result);
    }


    //返回上一级文件夹
    @GetMapping("/returnLastFolder")
    folder returnLastFolder(@RequestParam String folderId){
        return folderService.getParentFolderById(folderId);
    }

    //根据文件夹ID查询文件夹
    @GetMapping("/getFolder")
    FileStatus getFolder(@RequestParam String folderId) throws IOException {
        return folderService.selectFolderByFolderId(folderId);
    }


    //新建文件夹
    @PostMapping("/createFolder")
    public ResponseEntity<?> createFolder(@RequestBody folder folder) throws Exception {
        try{
            boolean success = folderService.createFolder(folder);
            if (success) {
                return ResponseEntity.ok("文件夹创建成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件夹创建失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
            }
        }


    //删除文件夹
    @DeleteMapping("/deleteFolder")
    public ResponseEntity<?> deleteFolder(@RequestParam String folderId) {
        try {
            boolean deleted = folderService.deleteFolder(folderId);
            if (deleted) {
                return new ResponseEntity<>("Folder deleted successfully.", HttpStatus.OK);
            } else {
                return new ResponseEntity<>("Failed to delete folder.", HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //重命名文件夹
    @PutMapping("/renameFolder")
    public ResponseEntity<?> renameFolder(@RequestParam String newName,@RequestParam String oldName,@RequestParam String folderId) {
        System.out.println(newName+" "+oldName+" "+folderId);
        try {
            folder renamedFolder = folderService.renameFolder(oldName,newName,folderId);
            return new ResponseEntity<>(renamedFolder, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    @PostMapping("/removeFolder")
    //移动文件夹
    public ResponseEntity<?> moveFolder(@RequestParam String folderId,@RequestParam String lastFolder){
        try{
            folder moveFolder = folderService.moveFolder(folderId,lastFolder);
            return new ResponseEntity<>(moveFolder, HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}